/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "pneumatic_muscle_control.h"
#include "motor_control.h"
#include "pc_interaction.h"
#include "can_cfg.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

#define task_run_test_flag 1
uint64_t taskrun_test[10] = 0;
extern float fAcc[3];
extern float fGyro[3];
extern float fAngle[3];
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint32_t tx_gui_event = 0; /* 定义一个事件接收变量 */ 
uint32_t deal_ang_event = 0; /* 定义一个事件接收变量 */ 
uint32_t ang_ctr_event = 0; /* 定义一个事件接收变量 */ 
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for wrist_ctr_task */
osThreadId_t wrist_ctr_taskHandle;
const osThreadAttr_t wrist_ctr_task_attributes = {
  .name = "wrist_ctr_task",
  .stack_size = 512 * 4,
  .priority = (osPriority_t) osPriorityBelowNormal,
};
/* Definitions for elbow_ctr_task */
osThreadId_t elbow_ctr_taskHandle;
const osThreadAttr_t elbow_ctr_task_attributes = {
  .name = "elbow_ctr_task",
  .stack_size = 512 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for gui_com_task */
osThreadId_t gui_com_taskHandle;
const osThreadAttr_t gui_com_task_attributes = {
  .name = "gui_com_task",
  .stack_size = 512 * 4,
  .priority = (osPriority_t) osPriorityAboveNormal,
};
/* Definitions for Mcontrol_task */
osThreadId_t Mcontrol_taskHandle;
const osThreadAttr_t Mcontrol_task_attributes = {
  .name = "Mcontrol_task",
  .stack_size = 512 * 4,
  .priority = (osPriority_t) osPriorityBelowNormal,
};
/* Definitions for other_task */
osThreadId_t other_taskHandle;
const osThreadAttr_t other_task_attributes = {
  .name = "other_task",
  .stack_size = 512 * 4,
  .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for joint_ang_sem */
osSemaphoreId_t joint_ang_semHandle;
const osSemaphoreAttr_t joint_ang_sem_attributes = {
  .name = "joint_ang_sem"
};
/* Definitions for Tx_Gui_event */
osEventFlagsId_t Tx_Gui_eventHandle;
const osEventFlagsAttr_t Tx_Gui_event_attributes = {
  .name = "Tx_Gui_event"
};
/* Definitions for ang_ctr_event */
osEventFlagsId_t ang_ctr_eventHandle;
const osEventFlagsAttr_t ang_ctr_event_attributes = {
  .name = "ang_ctr_event"
};
/* Definitions for deal_ang_event */
osEventFlagsId_t deal_ang_eventHandle;
const osEventFlagsAttr_t deal_ang_event_attributes = {
  .name = "deal_ang_event"
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void wrist_ctr_task_(void *argument);
void elbow_ctr_task_(void *argument);
void gui_com_task_(void *argument);
void Mcontrol_task_(void *argument);
void other_task_(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* Create the semaphores(s) */
  /* creation of joint_ang_sem */
  joint_ang_semHandle = osSemaphoreNew(1, 0, &joint_ang_sem_attributes);

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of wrist_ctr_task */
  wrist_ctr_taskHandle = osThreadNew(wrist_ctr_task_, NULL, &wrist_ctr_task_attributes);

  /* creation of elbow_ctr_task */
  elbow_ctr_taskHandle = osThreadNew(elbow_ctr_task_, NULL, &elbow_ctr_task_attributes);

  /* creation of gui_com_task */
  gui_com_taskHandle = osThreadNew(gui_com_task_, NULL, &gui_com_task_attributes);

  /* creation of Mcontrol_task */
  Mcontrol_taskHandle = osThreadNew(Mcontrol_task_, NULL, &Mcontrol_task_attributes);

  /* creation of other_task */
  other_taskHandle = osThreadNew(other_task_, NULL, &other_task_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* Create the event(s) */
  /* creation of Tx_Gui_event */
  Tx_Gui_eventHandle = osEventFlagsNew(&Tx_Gui_event_attributes);

  /* creation of ang_ctr_event */
  ang_ctr_eventHandle = osEventFlagsNew(&ang_ctr_event_attributes);

  /* creation of deal_ang_event */
  deal_ang_eventHandle = osEventFlagsNew(&deal_ang_event_attributes);

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_wrist_ctr_task_ */
/**
  * @brief  Function implementing the wrist_ctr_task thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_wrist_ctr_task_ */
void wrist_ctr_task_(void *argument)
{
  /* USER CODE BEGIN wrist_ctr_task_ */
  /* Infinite loop */
  for(;;)
  {
    // ppv_wrist_control();
    #if task_run_test_flag
    taskrun_test[0]++;
    Remote_map();
    #endif
    osDelay(15);
  }
  /* USER CODE END wrist_ctr_task_ */
}

/* USER CODE BEGIN Header_elbow_ctr_task_ */
/**
* @brief Function implementing the elbow_ctr_task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_elbow_ctr_task_ */
void elbow_ctr_task_(void *argument)
{
  /* USER CODE BEGIN elbow_ctr_task_ */
  /* Infinite loop */
  for(;;)
  {
    // ppv_elbow_control();
    #if task_run_test_flag
    taskrun_test[1]++;
    #endif
    osDelay(10);
  }
  /* USER CODE END elbow_ctr_task_ */
}

/* USER CODE BEGIN Header_gui_com_task_ */
/**
* @brief Function implementing the gui_com_task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_gui_com_task_ */
void gui_com_task_(void *argument)
{
  /* USER CODE BEGIN gui_com_task_ */
  /* Infinite loop */
  for(;;)
  {
    tx_gui_event = osEventFlagsWait(Tx_Gui_eventHandle,     /* 事件对象句柄 */ 
      tx_send_finish_event|tx_gui_time_event,               /* 接收任务感兴趣的事件 */ 
      osFlagsWaitAll,                                       /* 退出时清除事件位，同时满足感兴趣的所有事件 */ 
      osWaitForever);                                       /* 指定超时事件,一直等 */ 

    if((tx_gui_event & (tx_send_finish_event|tx_gui_time_event)) == (tx_send_finish_event|tx_gui_time_event)){
      tx_gui_event = 0;
      ToPC_dataPack();
    }
    #if task_run_test_flag
    taskrun_test[2]++;
    #endif
    osDelay(1);
  }
  /* USER CODE END gui_com_task_ */
}

/* USER CODE BEGIN Header_Mcontrol_task_ */
/**
* @brief Function implementing the Mcontrol_task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Mcontrol_task_ */
void Mcontrol_task_(void *argument)
{
  /* USER CODE BEGIN Mcontrol_task_ */
  /* Infinite loop */
  for(;;)
  {
    deal_ang_event = osEventFlagsWait(deal_ang_eventHandle,     /* 事件对象句柄 */ 
      deal_ang_time_event,             /* 接收任务感兴趣的事件 */ 
      osFlagsWaitAll,                 /* 退出时清除事件位，同时满足感兴趣的所有事件 */ 
      osWaitForever);                 /* 指定超时事件,一直等 */ 

    if(deal_ang_event & deal_ang_time_event == deal_ang_time_event){
      deal_ang_event = 0;
      canCallback_datadeal();
    }
    #if task_run_test_flag
    taskrun_test[3]++;
    #endif
    osDelay(1);
  }
  /* USER CODE END Mcontrol_task_ */
}

/* USER CODE BEGIN Header_other_task_ */
/**
* @brief Function implementing the other_task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_other_task_ */
void other_task_(void *argument)
{
  /* USER CODE BEGIN other_task_ */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END other_task_ */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

